package com.yx.yinshanjinniu.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.bean.ShopGoodsListBean;
import com.yx.yinshanjinniu.interfaces.OnItemClickListener;
import com.yx.yinshanjinniu.utils.GlideUtils;
import com.zc.mychart.utils.FormatUtil;

import java.util.ArrayList;
import java.util.List;


public class HomeGoodsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ShopGoodsListBean> shopGoodsListBeans = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setShopGoodsListBeans(List<ShopGoodsListBean> shopGoodsListBeans) {
        this.shopGoodsListBeans = shopGoodsListBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_goods, parent, false);
        return new HomeGoodsViewHolder(view, parent.getContext());
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (holder instanceof HomeGoodsViewHolder){
            if (onItemClickListener != null){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(shopGoodsListBeans, v, position, holder.getItemId());
                    }
                });
            }
            ((HomeGoodsViewHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return shopGoodsListBeans.size();
    }

    private class HomeGoodsViewHolder extends RecyclerView.ViewHolder{
        private final ImageView iv_goods_img;
        private final TextView tv_goods_name;
        private final TextView tv_goods_money;
        private Context mContext;
        public HomeGoodsViewHolder(@NonNull View itemView, Context context) {
            super(itemView);
            mContext = context;
            iv_goods_img = itemView.findViewById(R.id.iv_goods_img);
            tv_goods_name = itemView.findViewById(R.id.tv_goods_name);
            tv_goods_money = itemView.findViewById(R.id.tv_goods_money);
        }

        private void setData(int position){
            ShopGoodsListBean bean = shopGoodsListBeans.get(position);

            GlideUtils.showImageOptions(mContext, bean.goods_img, iv_goods_img);

            tv_goods_name.setText(bean.name);
            tv_goods_money.setText(FormatUtil.formatMoney(bean.price));
        }
    }
}
